package com.example.demo.day0227;

import com.sun.xml.internal.ws.api.server.SDDocument;

public class DemoGenerics<T> {
    private T key;
    public void setKey(T key){
        this.key = key;
    }
    public T getKey(){
        return this.key;
    }
}
class Test{
    public static void main(String[] args) {
        DemoGenerics<String> d1 = new DemoGenerics<String>();//在new DemoGenerics的对象时指定泛型的类型为string
        d1.setKey("xxxx");//对象使用setKey（T key）方法中key的形参就是string
        String s=d1.getKey();//返回值就由new对象确定返回值是string

        DemoGenerics<Integer> d2 = new DemoGenerics<Integer>();
        d2.setKey(1);
        Integer i = d2.getKey();

        DemoGenerics d3 = new DemoGenerics();//不指定泛型相当于指定了Object类型
        d3.setKey(new Object());
        Object obj = d3.getKey();

//        d1 = d2;//不同类型的泛型不能互相赋值
    }
}